package com.labichaoka.chaoka.ui.account;

import com.labichaoka.chaoka.entity.BaseResponse;
import com.labichaoka.chaoka.entity.LoginRequest;
import com.labichaoka.chaoka.entity.LoginResponse;
import com.labichaoka.chaoka.entity.SendCodeRequest;
import com.labichaoka.chaoka.utils.SharedPreferencesManager;

/**
 * Created by ruan on 2018/7/31.
 */

public class LoginPresenterImpl implements LoginPresenter,LoginInteractor.OnFinishedListener{
    private LoginView loginView;
    private LoginInteractor interactor;

    public LoginPresenterImpl(LoginView lView,LoginInteractor lInteractor){
        this.loginView=lView;
        this.interactor=lInteractor;
    }

    @Override
    public void getSmsCode(String phone, String imgCode) {
        if (loginView!=null){
            loginView.showProgress();
        }
        SendCodeRequest request=new SendCodeRequest();
        request.setPhone(phone);
        request.setImgVcode(imgCode);
        interactor.getSmsCode(request,this);
    }

    @Override
    public void login(LoginRequest request) {
        if (loginView!=null){
            loginView.showProgress();
        }
        interactor.login(request,this);
    }

    @Override
    public void onDestroy() {
        loginView=null;
    }

    @Override
    public void onGetCodeSuccess(BaseResponse response) {
        if (response.getCode().equals("-200")){
            loginView.showMessage(response.getMessage());
            //需要输入图形验证码
            loginView.showImgCode();
        }else {
            //验证码倒计时开启
            loginView.startCountDown();
        }

        loginView.hideProgress();
    }

    @Override
    public void onGetCodeFailed() {
        loginView.hideProgress();
    }

    @Override
    public void onLoginSuccess(LoginResponse response) {
        SharedPreferencesManager.getInstance().putString("token",response.getData());
        loginView.hideProgress();
        loginView.navigateToHome();
        loginView.countDownCancel();

    }

    @Override
    public void onLoginFailed() {
        loginView.hideProgress();
    }
}
